<?
	session_start();	
	error_reporting(E_ALL);
	include('conn.php');	
	$id = $_REQUEST['id'];
	
	switch($id)
	{
		case '1':
			$qry1 = "SELECT distinct year(ActivationDate) as yrs from STBtoCustomer";
			$qry = mysql_query($qry1);
			while($rows = mysql_fetch_array($qry))
				$data[] = $rows['yrs'];
			echo json_encode($data);
			break;
		
		case '2':			
			$yr = $_REQUEST['year'];
			$mon = $_REQUEST['month'];
			/*$qry1 = "select count(CustId) as totcust , RateperMonth
					from cableappdev.STBtoCustomer
					where CustId in
					(select CustId from STBtoCustomer where 
						(period_diff(date_format((case when isnull(`TerminationDate`) then '$yr-$mon-01' 
							when ( `TerminationDate` = '0000-00-00 00:00:00') then '$yr-$mon-01'
							else  `TerminationDate` end),'%Y%m'),
							date_format('$yr-$mon-01','%Y%m')) + 1)>'0'
						and
						(period_diff(date_format('$yr-$mon-01','%Y%m'),
							date_format((case when isnull(`ActivationDate`) then '$yr-$mon-01' 
							when ( `ActivationDate` = '0000-00-00 00:00:00') then '$yr-$mon-01'
							else  `ActivationDate` end),'%Y%m'))+ 1)>'0'
					)
					group by RateperMonth;";*/
			$qry1 = "select count(CustId) as totcust , RateperMonth	from cableappdev.STBtoCustomer 
			where `TerminationDate` is null or (month(`TerminationDate`) >= '$mon' and year(`TerminationDate`)>='$yr') 
			group by RateperMonth;";
			$qry = mysql_query($qry1);
			$data = array();
			$i=1;
			while($rows = mysql_fetch_array($qry))
			{												
				$data[] = array("Sr_NO" => $i++,									
					"custcnt" => $rows['totcust'],																
					"rpm" => $rows['RateperMonth']	,															
					"totalamt" => $rows['RateperMonth'] * $rows['totcust']										
				);
			}
			echo json_encode($data);
			break;
			
		default:
			echo "Please select an ID";
			break;
	}
?>